﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using System.Web;
using System.Data;

namespace DataAccess.Classes
{
    public class GioHang
    {
        #region Anh xa cac cot trong bang => thuoc tinh
        private string _MaGioHang;
        public string MaGioHang
        {
            get { return _MaGioHang; }
            set { _MaGioHang = value; }
        }

        private string _MaKhachHang;

        public string MaKhachHang
        {
            get { return _MaKhachHang; }
            set { _MaKhachHang = value; }
        }
        private string _MaPhuongThuc;

        public string MaPhuongThuc
        {
            get { return _MaPhuongThuc; }
            set { _MaPhuongThuc = value; }
        }
        private DateTime _NgayTao;

        public DateTime NgayTao
        {
            get { return _NgayTao; }
            set { _NgayTao = value; }
        }

        #endregion

        #region Ham
        public static List<HangHoa> Items()
        {
            return CBO.FillCollection<HangHoa>(DataProvider.Instance.ExecuteReader("ShoppingCart_Items", ShoppingCartId));
        }

        public static string TotalAmount()
        {
            return DataProvider.Instance.ExecuteScalar("ShoppingCart_TotalAmount", ShoppingCartId).ToString();
        }

        private static string ShoppingCartId 
        { 
            get 
            { 
                HttpContext context = HttpContext.Current;
                if (context.Request.Cookies["CartID"] != null)
                {
                    return context.Request.Cookies["CartID"].Value;
                }
                else
                {
                    string cartId = Guid.NewGuid().ToString();
                    HttpCookie cookie = new HttpCookie("CartID", cartId);
                    int howManyDays = GlobalConfiguration.CartPersistDays;
                    DateTime currentDate = DateTime.Now;
                    TimeSpan timeSpan = new TimeSpan(howManyDays, 0, 0, 0);
                    DateTime expirationDate = currentDate.Add(timeSpan);
                    cookie.Expires = expirationDate;
                    context.Response.Cookies.Add(cookie); return cartId;


                   
                }
            }

            
        }

        public static bool Remove(string maHang)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("GioHang_Xoa", ShoppingCartId, maHang);
                return rs > 0;
            }
            catch
            { return false; }
        }

        public static bool Add(string ma)
        {
            try
            {
                int rs = DataProvider.Instance.ExecuteNonQuery("GioHang_Them", ShoppingCartId, ma);
                return rs > 0;
            }
            catch
            { return false; }
        }

        public static int CreateOrder(string firstName, string lastName, string email, string phone, string address) 
        {
            try 
            {
                HttpContext context = HttpContext.Current;
                string username = context.User.Identity.Name;

                IDataReader reader = DataProvider.Instance.ExecuteReader("Order_Check",
                    username, firstName, lastName,
                    address, phone, ShoppingCartId, email);
                reader.Read();
                int count = (int)Math.Ceiling((double)reader.GetInt32(0));
                if (count >= 1)
                {
                    reader = DataProvider.Instance.ExecuteReader("Order_take",
                    username, firstName, lastName,
                    address, phone, ShoppingCartId, email);
                    reader.Read();
                    int kq = (int)Math.Ceiling((double)reader.GetInt32(0));
                    return kq;
                }
                else
                {
                    int ma = 0;
                    object rs = DataProvider.Instance.ExecuteNonQueryWithOutput(
                       "@Ma", "Order_Create",
                        username, firstName, lastName,
                        address, phone, ShoppingCartId, email, ma);
                    return Convert.ToInt32(rs);
                }
            }
            catch { return -1; }
        }
        #endregion
    }
}
